package graph;

import java.util.HashMap;

import exception.ImpossibleTaskConfigurationException;

import task_system.Task;

public class TaskConfiguration_Scheduler extends TaskConfiguration {

	public TaskConfiguration_Scheduler(int p, int d, int c, Task t) throws ImpossibleTaskConfigurationException {
		super(p, d, c, t);
		this.owner = TaskConfiguration.SCHEDULER;
	}
	
	private TaskConfiguration_System scheduled() throws ImpossibleTaskConfigurationException{
		int mP = Math.max(p-1, 0);
		int mD = Math.max(d-1, 0);
		int mC = Math.max(c-1, 0);
		if(task.compatibleConfiguration(mP, mD, mC))
				return new TaskConfiguration_System(mP, mD, mC, task);
		return null;
	}
	
	private TaskConfiguration_System nonScheduled() throws ImpossibleTaskConfigurationException{
		
		int mP = Math.max(p-1, 0);
		int mD = Math.max(d-1, 0);
		int mC = c;
		if(task.compatibleConfiguration(mP, mD, mC))
			return new TaskConfiguration_System(mP, mD, mC, task);
		return null;
	}
	
	public HashMap<String, TaskConfiguration> successors(){
		HashMap<String, TaskConfiguration> successors = new HashMap<String, TaskConfiguration>();

		try {
			successors.put("Scheduled", scheduled());
			successors.put("Not Scheduled", nonScheduled());
		} catch (ImpossibleTaskConfigurationException e) {
			e.printStackTrace();
		}
		
		return successors;
	}

	private TaskConfiguration_System activated() throws ImpossibleTaskConfigurationException{
		if( c==task.C() && d==task.D() && p==task.P() )
			if(task.compatibleConfiguration(0,0,0))
				return new TaskConfiguration_System(0,0,0, task);
		return null;
	}
	
	private TaskConfiguration_System nonActivated() throws ImpossibleTaskConfigurationException{
		if (c==task.C() && d==task.D() && p==task.P())
			return null;
		if(task.compatibleConfiguration(p,d,c))
			return new TaskConfiguration_System(p,d,c, task);
		return null;

	}
	
	public HashMap<String, TaskConfiguration> predecessors(){
		HashMap<String, TaskConfiguration> predecessors = new HashMap<String, TaskConfiguration>();
		
		try {
			predecessors.put("Activated", activated());
			predecessors.put("Not Activated", nonActivated());
		} catch (ImpossibleTaskConfigurationException e) {
			e.printStackTrace();
		}
		return predecessors;
	}
}
